package com.swufestu.hello;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.TextView;

public class CalcActivity extends AppCompatActivity {

    private static final String TAG = "CalcActivity";
    private EditText input;
    private TextView show;
    private TextView title;
    private float rate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calc);
//        String titleStr = item.getCname();
//        String detailStr = item.getCval();
        title = findViewById(R.id.calc_title);
        input = findViewById(R.id.calc_rmb);
        show = findViewById(R.id.calc_result);

        Intent intent = getIntent();
        String cname = intent.getStringExtra("cname_key");
        title.setText(cname);
        String cval = intent.getStringExtra("cval_key");
        title.setText(cval);
        String title=getIntent().getStringExtra("title");
        rate=getIntent().getFloatExtra("rate",0f);
        Log.i(TAG, "onItemClick: title=" + title);
        Log.i(TAG, "onItemClick: rate=" + rate);
        ((TextView)findViewById(R.id.calc_title)).setText(title);
        input=(EditText)findViewById(R.id.calc_rmb);
        input.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                TextView show = (TextView) CalcActivity.this.findViewById(R.id.calc_result);
                if(s.length()>0){
                    float val = Float.parseFloat(s.toString());
                    show.setText(val + "RMB==>" + (100/rate*val));
                }else{
                    show.setText("");
                }
            }
        });
    }
}